var Websocket = require("websocket").server;
var http = require("http")

//创建http服务器
var httpServer = http.createServer().listen(8088,function (){
    console.log("http://127.0.0.1:8088");
});

//创建websocket对象
var wsServer = new Websocket({
    httpServer:httpServer,
    autoAcceptConnections: false
});

var conArr = [];
//监听请求事件
wsServer.on('request',function (request){
    //得到链接实例
    var connection = request.accept();
    conArr.push(connection);

    //监听消息
    connection.on('message',function (msg){
        //console.log(msg);//{ type: 'utf8', utf8Data: '张辽' }
        conArr.forEach(n => {
            n.send("你好，"+msg.utf8Data);
        });
    });
});